package com.neusoft.neu24.his.hisapp;

import com.neusoft.neu24.his.hisapp.entity.Patient;
import com.neusoft.neu24.his.hisapp.service.IPatientService;
import com.neusoft.neu24.his.hisapp.service.impl.PatientServiceImpl;
import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Pair;
import javafx.util.StringConverter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class HelloApplication_ListView_Demo extends Application {

    private IPatientService patientService =new PatientServiceImpl();

    private  List<Patient> patientList = null;

    @Override
    public void start(Stage stage) throws IOException {
//        普通的内存集合，不具备显示能力
        patientList =  patientService.selectAllPatient();
//        可视化集合
        ObservableList<Patient> patientObservableList = FXCollections.observableArrayList(patientList);

        ReadOnlyObjectProperty<ObservableList<Patient>> playersProperty =
                new SimpleObjectProperty<>(patientObservableList);

        ReadOnlyObjectProperty<FilteredList<Patient>> viewablePlayersProperty =
                new SimpleObjectProperty<FilteredList<Patient>>(
                        new FilteredList<>(playersProperty.get()
                        ));

        ObjectProperty<Predicate<? super Patient>> filterProperty =
                viewablePlayersProperty.get().predicateProperty();

//  在Java中，可以把一个方法作为另一个方法的实参进行传递
        EventHandler<ActionEvent> toggleHandler = (event) -> {
            ToggleButton tb = (ToggleButton)event.getSource();
            Predicate<Patient> filter = (Predicate<Patient>)tb.getUserData();
            filterProperty.set( filter );
        };

        ToggleGroup filterTG = new ToggleGroup();

        ToggleButton tbShowAll = new ToggleButton("Show All");
        tbShowAll.setSelected(true);
        tbShowAll.setToggleGroup( filterTG );
        tbShowAll.setUserData( (Predicate<? super Patient>) (Patient p) -> true);
        tbShowAll.setOnAction( toggleHandler );






//        处理按钮组
        List<ToggleButton> toggleButtons = patientList.stream()
                .map(Patient::getStatus)
                .distinct()
                .map( (status) ->{
                    ToggleButton tb =new ToggleButton();
                    tb.setToggleGroup(filterTG);
                    tb.setUserData(  (Predicate<? super Patient>) (Patient p) -> p.getStatus().equals(status)  );
                    tb.setOnAction( toggleHandler);
                    tb.setText(status);
                    return  tb;
                })
                .collect( Collectors.toList()  );

        HBox btns =new HBox();
        btns.getChildren().addAll(tbShowAll);
        btns.getChildren().addAll(toggleButtons     );
        ListView<Patient> listView = new ListView<>();
        listView.itemsProperty().bind( viewablePlayersProperty );
        VBox root = new VBox();
        root.getChildren().addAll(btns,listView);
        Scene scene =new Scene( root, 400,500);
        stage.setScene(scene);
        stage.show();
        stage.setTitle("ListView_DEMO");
    }
    public static void main(String[] args) {
        launch();
    }
}